<?php 
/* ================================================================================
 * LWAdmin	 
 *
 * applications.php: This is the applications file for Living Web.
 *
 * Author: Eric Hendrickson (enhendrickson@liberty.edu)
 * Date: 2/18/05
   ================================================================================ */
	
	global $SSO, $Security, $AppID, $User, $DB;
	//Check to see if there is a valid session 
	$SessionInfo = $SSO->isValidSession("index.php?action=login", $SSO->getSessionInformation());	
		
	//Check to see if the user has rights to be on LWAdmin
	$PageRights = array('ADMIN');
	$Security->ValidateUser($SessionInfo['username'], $AppID, $PageRights, 1);
		
	//Grab the user info
	$UserInfo = $User->GetUserInfo($SessionInfo['username']);
?>

<table cellpadding="0" cellspacing="0"  border="0">
	<tr>
		<td>
			<?php 
				if(isset($_GET['id']) && $_GET['id'] != "")
				{
					if($_GET['id'] != 'NEW')
					{
						ChoosenApp();
					}
					else
					{
						NewApp();
					}
				}
				elseif(isset($_GET['save_id']))
				{
					SaveApp();
				}
				else
				{
					ChooseApp();
				}
				
				if(isset($_GET['a']) && $_GET['a'] == 'delete')
				{
					if(isset($_POST['txtAppID']))
					{
						global $DB;
						$DB->query("DELETE FROM apps WHERE appid = '" . $_POST['txtAppID'] . "'");
						
						header("Location: index.php?action=applications");
					}
				}
?>
		</td>
	</tr>
</table>


<?php 
	function NewApp()
	{
		BuildForm('','','','','');
	}	

	function SaveApp()
	{
		global $DB;
		
		if($_POST['txtAppID'] != $_GET['save_id'])
		{
			$db_string = $DB->compile_db_insert_string( 
			array (
				'AppID'		=> $_POST['txtAppID'],
				'AppName'		=> $_POST['txtAppName'],
				'AppPath'   => $_POST['txtAppPath'],
				'AppPurpose'		=> $_POST['txtAppPurpose'],
				'AppDescription'		=> $_POST['txtAppDescription'])
				);
			
			//insert the session into the sessions table
			$DB->query("INSERT INTO apps (".$db_string['FIELD_NAMES'].") VALUES (".$db_string['FIELD_VALUES'].")");
		}
		else
		{
			$db_string = $DB->compile_db_update_string( 
			array (
				'AppID'		=> $_POST['txtAppID'],
				'AppName'		=> $_POST['txtAppName'],
				'AppPath'   => $_POST['txtAppPath'],
				'AppPurpose'		=> $_POST['txtAppPurpose'],
				'AppDescription'		=> $_POST['txtAppDescription'])
				);
			
			//insert the session into the sessions table
			$DB->query("UPDATE apps SET " . $db_string . " WHERE AppID = '" . $_GET['save_id'] . "'");
		}
		
		header("Location: index.php?action=applications");
		exit();
	}	

	function BuildForm($app_id,$app_name,$app_path,$app_description,$app_purpose)
	{
		print '
		<form action="index.php?action=applications&amp;save_id=' . $app_id .  '" method="post" name="frmApplications">
			<table cellpadding="0" cellspacing="0" border="0" width="100%">
				<tr>
					<td>
						<table cellpadding="0" cellspacing="10" border="0">
							<tr>
								<td>
									<span class="TB1">Name</span>&nbsp;&nbsp;&nbsp;
								</td>
								<td>
									<input name="txtAppName" class="T1" size="30" type="text" value="' . $app_name . '">
								</td>
							</tr>
							<tr>
								<td>
									<span class="TB1">Application ID</span>&nbsp;&nbsp;&nbsp;
								</td>
								<td>
									<input name="txtAppID" class="T1" size="15" type="text" value="' . $app_id . '">
								</td>
							</tr>
							<tr>
								<td valign="top">
									<span class="TB1">Path</span>
								</td>
								<td>
									<input type="text" size="50" name="txtAppPath" class="T1" value="' . $app_path . '"';
									 
						if($app_id != '')
						{
							print 'readonly';
						}									 
							print '>
								</td>
							</tr>
							<tr>
								<td valign="top">
									<span class="TB1">Description</span>
								</td>
								<td>
									<textarea name="txtAppDescription" class="T1" cols="50" rows="3">' . $app_description . '</textarea>
								</td>
							</tr>
							<tr>
								<td valign="top">
									<span class="TB1">Purpose</span>
								</td>
								<td>
									<textarea name="txtAppPurpose" class="T1" cols="50" rows="3">' . $app_purpose . '</textarea>
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td>
						<table cellpadding="0" cellspacing="0" align="center" border="0">
							<tr>
								<td>
									<input name="btnSave" type="submit" class="B" value="Save">
								</td>
								<td>&nbsp;&nbsp;</td>
								<td>
									<input name="btnDelete" type="button" class="B" value="Delete" onClick="if(confirm(\'Are you sure you would like to delete this application?\')){this.form.action=\'index.php?action=applications&amp;a=delete\';this.form.submit();}">
								</td>
								<td>&nbsp;&nbsp;</td>
								<td>
									<input name="btnBack" type="button" class="B" value="Back" onClick="history.back(-1);">
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
			</form>';
	}
	

	function ChoosenApp()
	{
		global $DB;
		$DB->query("SELECT * FROM apps WHERE AppID = '" . $_GET['id'] . "'");
		$row = $DB->fetch_row();
		
		if(!empty($row))
		{
			BuildForm($row['AppID'],$row['AppName'],$row['AppPath'],$row['AppDescription'],$row['AppPurpose']);
		}
		else
		{
			die("The id provided is not a valid application id.  If this is an unexpected error please contact the site administrator."); 
		}
	}

	function ChooseApp()
	{
		global $DB;
		
		$Apps = '<table cellpadding="0" cellspacing="0" align="center" border="0">
		<tr><td>&nbsp;</td></tr><tr><td><a href="index.php?action=applications&id=NEW" class="LB1">New Application</a><hr></td></tr>
		<tr><td><span class="TB1" style="color:#555555;">Edit Application</span></td></tr>';

		$row_count = 1;
		$DB->query("SELECT * FROM apps ORDER BY AppName");
		while($row = $DB->fetch_row())
		{
			$Apps = $Apps . '<tr><td>&nbsp;&nbsp;&raquo;&nbsp;<a href="index.php?action=applications&amp;id=' . $row['AppID'] . '" class="L1">' . $row['AppName'] . '</a>&nbsp;&nbsp;&nbsp;</td></tr>';
		}
		
		$Apps = $Apps . '<tr><td>&nbsp;</td></tr></table>';
		
		print '
			<table cellpadding="0" cellspacing="0" align="center" border="0" width="100%">
				<tr>
					<td>' 
					. $Apps . 
					'</td>
				</tr>
			</table>
			</form>';
	}
?>
